//
//  BillAddPackageListVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/11/11.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "BillAddPackageListVC.h"
#import "BillAddPackageModel.h"
#import "BillAddPackageListCell.h"
#import "ScanGoodListVC.h"
#import "ShouGongGoodListVC.h"
#import "MyShipmentDetailVC.h"

@interface BillAddPackageListVC () <UITableViewDelegate,UITableViewDataSource> {
    int page;
}

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <BillAddPackageModel *>*dataArray;

@end

@implementation BillAddPackageListVC

-(NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH - BottomHeight - NaviHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"运单添加包装";
    page = 1;
    [self.view addSubview:self.tableView];
    [self requestForList];
}

- (void)requestForList {
    [HttpRequest PostWithApi:ReceiptDeliveryBillAddPackageList params:@{@"page":@(page)} progress:^(NSProgress * _Nonnull progress) {
            
    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.dataArray removeAllObjects];
                self.dataArray = [BillAddPackageModel mj_objectArrayWithKeyValuesArray:[responseObject objectForKey:@"list"]];
            }else {
                NSArray *array = [BillAddPackageModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.dataArray addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BillAddPackageListCell *cell = [BillAddPackageListCell BillAddPackageListCellWithTableView:tableView];
    BillAddPackageModel *model = self.dataArray[indexPath.row];
    cell.model = model;
    __weak_self;
    cell.scanBlock = ^{
        ScanGoodListVC *vc = [ScanGoodListVC new];
        vc.wa_id = model.wa_id;
        vc.isJieChu = NO;
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    cell.shouDongBlock = ^{
        ShouGongGoodListVC *vc = [ShouGongGoodListVC new];
        vc.wa_id = model.wa_id;
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    cell.jieChuBlock = ^{
        ScanGoodListVC *vc = [ScanGoodListVC new];
        vc.wa_id = model.wa_id;
        vc.isJieChu = YES;
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    cell.detailBlock = ^{
        MyShipmentDetailVC *vc = [MyShipmentDetailVC new];
        vc.wa_id = model.wa_id;
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    BillAddPackageListCell *cell = [BillAddPackageListCell BillAddPackageListCellWithTableView:tableView];
    cell.model = self.dataArray[indexPath.row];
    return cell.height;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

@end
